
"""
/*
 * @Author: wevsmy 
 * @Date: 2017-10-30 19:04:31 
 * @Last Modified by: wevsmy
 * @Last Modified time: 2017-10-30 19:17:17
 */
"""

import sys
import time
import gevent

from gevent import socket,monkey
monkey.patch_all()


def handle_request(conn,adder):
    print("-----客户端已连接[%s]-----"%str(adder))
    while True:
        data = conn.recv(1024)
        if not data:
            print("-----[%s]客户端已经关闭-----"%str(adder))
            conn.close()
            break
        print("receive:", data.decode("utf-8"))
        conn.send(str("HTTP/1.1 200 OK\r\n").encode("utf-8"))


def server(port):
    server_socket = socket.socket()
    server_socket.bind(("", port))
    server_socket.listen(5)
    while True:
        client, client_adder = server_socket.accept()
        gevent.spawn(handle_request, client,client_adder)


if __name__ == '__main__':
    server(8081)